﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class BOMItemRepository
    {
        // 添加BOMItem
        public int AddBOMItem(BOMItem bomItem)
        {
            string sql = @"INSERT INTO bomitem 
                            (BomId, ProductId, Sequence, MaterialType, 
                             DetailedMaterialType, MaterialNumber, MaterialDescription) 
                           VALUES 
                            (@BomId, @ProductId, @Sequence, @MaterialType, 
                             @DetailedMaterialType, @MaterialNumber, @MaterialDescription)";

            MySqlParameter[] parameters = {
                new MySqlParameter("@BomId", bomItem.BomId),
                new MySqlParameter("@ProductId", bomItem.ProductId),
                new MySqlParameter("@Sequence", bomItem.Sequence),
                new MySqlParameter("@MaterialType", bomItem.MaterialType),
                new MySqlParameter("@DetailedMaterialType", bomItem.DetailedMaterialType),
                new MySqlParameter("@MaterialNumber", bomItem.MaterialNumber),
                new MySqlParameter("@MaterialDescription", bomItem.MaterialDescription)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 删除BOMItem
        public int DeleteBOMItem(int bomItemId)
        {
            string sql = "DELETE FROM bomitem WHERE BOMItemId = @BOMItemId";
            MySqlParameter[] parameters = { new MySqlParameter("@BOMItemId", bomItemId) };
            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 更新BOMItem信息
        public int UpdateBOMItem(BOMItem bomItem)
        {
            string sql = @"UPDATE bomitem 
                            SET BomId = @BomId, 
                                ProductId = @ProductId,
                                Sequence = @Sequence,
                                MaterialType = @MaterialType,
                                DetailedMaterialType = @DetailedMaterialType,
                                MaterialNumber = @MaterialNumber,
                                MaterialDescription = @MaterialDescription
                          WHERE BOMItemId = @BOMItemId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@BomId", bomItem.BomId),
                new MySqlParameter("@ProductId", bomItem.ProductId),
                new MySqlParameter("@Sequence", bomItem.Sequence),
                new MySqlParameter("@MaterialType", bomItem.MaterialType),
                new MySqlParameter("@DetailedMaterialType", bomItem.DetailedMaterialType),
                new MySqlParameter("@MaterialNumber", bomItem.MaterialNumber),
                new MySqlParameter("@MaterialDescription", bomItem.MaterialDescription),
                new MySqlParameter("@BOMItemId", bomItem.BOMItemId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 根据BomId查询BOMItems
        public List<BOMItem> GetBOMItemsByBomId(int bomId)
        {
            string sql = "SELECT * FROM bomitem WHERE BomId = @BomId ORDER BY Sequence";
            MySqlParameter[] parameters = { new MySqlParameter("@BomId", bomId) };
            return DBHelper.ExecuteQuery(sql, parameters, MapBOMItem);
        }

        // 映射数据库查询结果到 BOMItem 对象
        private BOMItem MapBOMItem(MySqlDataReader reader)
        {
            return new BOMItem
            {
                BOMItemId = (int)reader["BOMItemId"],
                BomId = (int)reader["BomId"],
                ProductId = (int)reader["ProductId"],
                Sequence = (int)reader["Sequence"],
                MaterialType = reader["MaterialType"].ToString(),
                DetailedMaterialType = reader["DetailedMaterialType"].ToString(),
                MaterialNumber = reader["MaterialNumber"].ToString(),
                MaterialDescription = reader["MaterialDescription"].ToString()
            };
        }
    }
}